package com.mzy.linear_struct.hot100.回溯;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/07/26/上午9:21
 * @Description:
 */
public class Pro46全排列 {

    private List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> ans = new ArrayList<>();
        Deque<Integer> path = new LinkedList<>();
        boolean[] used = new boolean[nums.length];
        backtracking(nums, used, path, ans);


        return ans;
    }

    private void backtracking(int[] nums, boolean[] used, Deque<Integer> path, List<List<Integer>> ans) {
        if (path.size() == nums.length) {
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            if (!used[i]) {
                path.addLast(nums[i]);
                used[i] = true;
                backtracking(nums, used, path, ans);
                used[i] = false;
                path.removeLast();
            }
        }
    }


    public static void main(String[] args) {
        Pro46全排列 pro = new Pro46全排列();
        int[] nums = {1, 2, 3};
        System.out.println(pro.permute(nums));
    }
}






















